﻿using System;
using System.Data;
using MailClient;
using MailClient.Data;
using MailClient.Handlers;

namespace MailClient.Commands
{
    class contact : Query, ICommand, IDisposable
    {
        private Handlers.Command _parameter;
        private Handlers.designator _designator;
        public Handlers.designator Designator
        {
            get { return _designator; }
            set { _designator = Designator; }
        }
        public contact(Command com, string[] query)
        {
            _query = query;
            Process();

        }

        private string[] _query;

        private MailMessage _msg;

        private void Process()
        {

        }

        public void Dispose() { }

        public MailMessage MsgReply { get { return _msg; } }

        public bool CommitData() { return false; }

        public bool Command(Job job)
        {
            string preamble = "";
            string table = @"<TABLE border=""0"" cellpadding=""1"">
                                <TR>
                                    <TD bgcolor=""#FFFFFF"" align=""center"" width=""500"" valign=""middle"">
                                        <TABLE border=""0"" cols=""2"" style=""font-size: 9pt; color: black; font-family: verdana, helvetica"" width=""100%"" cellspacing=""2"" cellpadding=""0"">";
            preamble = "Information here is accurate as we can make it.<br />Please reply or notify us if you believe something is incorrect<br />";
            foreach (string s in _query)
            {

                DataRow[] rows = TryQuery(job.GetJobType(s), s);
                if(rows != null)
                foreach (DataRow row in rows)
                {
                    #region Job Person
                    if (job.GetJobType(s) == JobType.Person)
                    {
                        string[] fields = { "first_name", "last_name", "direct_phone", "mobile_phone", "email" };

                        foreach (string col in fields)
                        {
                            table += CreateHtmlTableRow(row, col);
                        }
                        fields[0] = row["contact_id"].ToString();
                        DataRow[] JobRows = CrossMap("project_contacts", "contact_id=" + "'" + fields[0] + "'");

                        foreach (DataRow JobRow in JobRows)
                        {
                            DataRow[] StrRows = CrossMap("projects", "str_id=" + "'" + JobRow["str_id"].ToString() + "'");

                            foreach (DataRow StrRow in StrRows)
                            {

                                table += String.Format(@" <TR id=""{0}"">
                        <TD valign=""top"" bgcolor=""#FFFFFF"" align=""right"">
                          <DIV id=""{0}"" style=""font-weight:bold"">
                            <NOBR>{0}</NOBR>
                          </DIV>
                        </TD>
                        <TD>&nbsp;&nbsp;&nbsp;</TD>
                        <TD valign=""top"">
                          {1}
                        </TD>
                      </TR>", "Store: ", StrRow["str_num"].ToString() + " " + StrRow["str_city"].ToString() + ", " + StrRow["str_St"].ToString());
                            }
                        }
                    }
                    #endregion
                    #region Job Store
                    else if (job.GetJobType(s) == JobType.Store)
                    {
                        string[] fields = { "str_num", "str_city", "str_st" };
                        int i = 0;
                        DataTable JobRoles = ContactsData.Tables["job_roles"];
                        
                        foreach (string col in fields)
                        {
                            table += CreateHtmlTableRow(row, fields[i]);
                            i++;
                        }
                        fields[0] = row["str_id"].ToString();
                      // select all rows matching a str_id, this is all contacts for the store
                        DataRow[] ContactRows = CrossMap("project_contacts", "str_id=" + "'" + fields[0] + "'");

                        foreach (DataRow ContactRow in ContactRows)
                        {
                            // select all rows match contact_id, this should only return a single contact
                            DataRow[] A_ContactRows = CrossMap("contacts", "contact_id=" + "'" + ContactRow["contact_id"].ToString() + "'");
                              DataRow[] jobrole = JobRoles.Select("job_role_id = '" + ContactRow["job_role_id"].ToString() + "'");

                            // just in case there are more than one contact ids
                            foreach (DataRow ThisContactRow in A_ContactRows)
                            {
                              
                                DataRow ThisJobRole = jobrole[0];
                               
                                table += String.Format(@" <TR id=""{0}"">
                        <TD valign=""top"" bgcolor=""#FFFFFF"" align=""right"">
                          <DIV id=""{0}"" style=""font-weight:bold"">
                            <NOBR>{0}</NOBR>
                          </DIV>
                        </TD>
                        <TD>&nbsp;&nbsp;&nbsp;</TD>
                        <TD valign=""top"">
                          {1}
                        </TD>
                      </TR>", ThisJobRole["job_role"].ToString() + ": ", ThisContactRow["FullName"].ToString() + ", " + ThisContactRow["email"].ToString());
                            }
                        }
                    }

                    /*
                     * columns of contacts: contact_id first_name, middle_name, last_name, job_title, company_id, direct_phone, mobile_phone, email
                     * columns of proects: str_id, str_num, str_city, str_st
                     */
                    #endregion
                    table += "<TR>&nbsp;</TR>";
                }

            }

            table += "</TABLE></TD></TR></TABLE>";
        



            _msg = new MailMessage();
            _msg.Subject = string.Format("RE: contacts for {0}", job.TestString);
            _msg.Body = preamble + table;
            _msg.IsBodyHtml = true;
            var from = new System.Net.Mail.MailAddress("rha@rhaaia.com", "RHA Email");
            _msg.To.Add(job.ReplyTo);
            _msg.Sender = from;
            return true;
        }


    }
}
